{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b301db305b2e03a4",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "学习目标\n",
    "- 知道Pandas的Series类型\n",
    "- 掌握Pandas的Dataframe类型\n",
    "- 了解Pandas的MultiIndex与panel类型"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "16b439d1ed180b3d",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "Pandas中一共有三种数据类型，分别为：Series、DataFrame和MultiIndex（老版本中叫Panel ）。\n",
    "其中Series是一维数据类型，DataFrame是二维的表格型数据类型，MultiIndex是三维的数据类型。\n",
    "# 1.Series\n",
    "Series是一个类似于一维数组的数据类型，它能够保存任何类型的数据，比如整数、字符串、浮点数等，主要由一组数据和与之相关的索引两部分构成。\n",
    "## 1.1 Series的创建\n",
    "### 全新创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 164,
   "id": "67b501e677c0b58",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:18.790049400Z",
     "start_time": "2024-02-22T06:21:18.615860500Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "Series([], dtype: object)"
     },
     "execution_count": 164,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入pandas\n",
    "import pandas as pd\n",
    "\n",
    "pd.Series(data=None, index=None, dtype=None)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c6172f32e3f8fa27",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "- 参数：\n",
    "    - data：传入的数据，可以是ndarray、list等\n",
    "    - index：索引，必须是唯一的，且与数据的长度相等。如果没有传入索引参数，则默认会自动创建一个从0-N的整数索引。\n",
    "    - dtype：数据的 类型\n",
    "### 通过已有数据创建\n",
    "- 指定内容，默认索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 165,
   "id": "8bed47503c5e53c9",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:18.815672400Z",
     "start_time": "2024-02-22T06:21:18.794545700Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "0    0\n1    1\n2    2\n3    3\n4    4\n5    5\n6    6\n7    7\n8    8\n9    9\ndtype: int32"
     },
     "execution_count": 165,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "pd.Series(np.arange(10))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "50ecb6f78a181f1c",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "- 通过字典数据创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "id": "e225150c0c8f11b5",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:18.860130100Z",
     "start_time": "2024-02-22T06:21:18.818671700Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "red        100\nblue       200\ngreen      500\nyellow    1000\ndtype: int64"
     },
     "execution_count": 166,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "color_count = pd.Series({'red':100, 'blue':200, 'green': 500, 'yellow':1000})\n",
    "color_count"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b64c0544d5418fc",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 1.2 Series的属性\n",
    "为了更方便地操作Series对象中的索引和数据，Series中提供了两个属性index和values\n",
    "- index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "id": "93798b543c645358",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:18.863646200Z",
     "start_time": "2024-02-22T06:21:18.832089900Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['red', 'blue', 'green', 'yellow'], dtype='object')"
     },
     "execution_count": 167,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "color_count.index\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4dc1c0d7ea6949cb",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "- values\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "id": "8b6e55cf888fec72",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:18.918877Z",
     "start_time": "2024-02-22T06:21:18.865646500Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([ 100,  200,  500, 1000], dtype=int64)"
     },
     "execution_count": 168,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "color_count.values\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b9e788ab7eae4b8",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "也可以使用索引来获取数据："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "id": "1e3277b8e4e1da5",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:18.953907900Z",
     "start_time": "2024-02-22T06:21:18.911345600Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "500"
     },
     "execution_count": 169,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# color_count[2] # FutureWarning: Series.__getitem__ treating keys as positions is deprecated.\n",
    "color_count.iloc[2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "46af315b6304fddc",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "# 2.DataFrame\n",
    "DataFrame是一个类似于二维数组或表格(如excel)的对象，既有行索引，又有列索引\n",
    "- 行索引，表明不同行，横向索引，叫index，0轴，axis=0\n",
    "- 列索引，表名不同列，纵向索引，叫columns，1轴，axis=1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "36105a36f092ad71",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 2.1 DataFrame的创建\n",
    "### 全新创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "id": "63abf81f344b98f5",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:18.972349100Z",
     "start_time": "2024-02-22T06:21:18.946350700Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "Empty DataFrame\nColumns: []\nIndex: []",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 170,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入pandas\n",
    "import pandas as pd\n",
    "\n",
    "pd.DataFrame(data=None, index=None, columns=None)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "eba8a329840946ff",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "- 参数\n",
    "    - index：行标签。如果没有传入索引参数，则默认会自动创建一个从0-N的整数索引。\n",
    "    - columns：列标签。如果没有传入索引参数，则默认会自动创建一个从0-N的整数索引。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bf575266da6cb4e1",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### 通过已有数据创建\n",
    "#### 举例一："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "id": "da7401465e18d008",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:19.013301300Z",
     "start_time": "2024-02-22T06:21:18.976745200Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "          0         1         2\n0  0.802410 -0.143496 -0.109050\n1 -2.471808 -1.853833  0.374201",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.802410</td>\n      <td>-0.143496</td>\n      <td>-0.109050</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>-2.471808</td>\n      <td>-1.853833</td>\n      <td>0.374201</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 171,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(np.random.randn(2,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "id": "a654edf59325df8c",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:19.014275700Z",
     "start_time": "2024-02-22T06:21:18.980987900Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[-0.05338989, -0.00326219,  0.16441391],\n       [ 1.70138998, -0.43072268,  1.67420581]])"
     },
     "execution_count": 172,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.array(np.random.randn(2,3)) #回忆咱们在前面直接使用np创建的数组显示方式，比较两者的区别。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1a740fd911cbcf61",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "#### 举例二：创建学生成绩表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 173,
   "id": "4dbf3c10f13fe6d2",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:19.066912100Z",
     "start_time": "2024-02-22T06:21:19.015275600Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[75, 45, 52, 89, 53],\n       [86, 64, 76, 47, 95],\n       [93, 86, 47, 73, 71],\n       [60, 70, 68, 40, 70],\n       [80, 96, 86, 64, 43],\n       [60, 53, 77, 57, 76],\n       [44, 71, 87, 46, 67],\n       [74, 49, 80, 99, 71],\n       [62, 84, 94, 84, 74],\n       [69, 97, 90, 93, 89]])"
     },
     "execution_count": 173,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 生成10名同学，5门功课的数据\n",
    "score = np.random.randint(40, 100, (10, 5)) # np创建\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "id": "bacdeb1d3aea106a",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:19.106127800Z",
     "start_time": "2024-02-22T06:21:19.041816600Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "    0   1   2   3   4\n0  75  45  52  89  53\n1  86  64  76  47  95\n2  93  86  47  73  71\n3  60  70  68  40  70\n4  80  96  86  64  43\n5  60  53  77  57  76\n6  44  71  87  46  67\n7  74  49  80  99  71\n8  62  84  94  84  74\n9  69  97  90  93  89",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>2</th>\n      <th>3</th>\n      <th>4</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>75</td>\n      <td>45</td>\n      <td>52</td>\n      <td>89</td>\n      <td>53</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>86</td>\n      <td>64</td>\n      <td>76</td>\n      <td>47</td>\n      <td>95</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>93</td>\n      <td>86</td>\n      <td>47</td>\n      <td>73</td>\n      <td>71</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>60</td>\n      <td>70</td>\n      <td>68</td>\n      <td>40</td>\n      <td>70</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>80</td>\n      <td>96</td>\n      <td>86</td>\n      <td>64</td>\n      <td>43</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>60</td>\n      <td>53</td>\n      <td>77</td>\n      <td>57</td>\n      <td>76</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>44</td>\n      <td>71</td>\n      <td>87</td>\n      <td>46</td>\n      <td>67</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>74</td>\n      <td>49</td>\n      <td>80</td>\n      <td>99</td>\n      <td>71</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>62</td>\n      <td>84</td>\n      <td>94</td>\n      <td>84</td>\n      <td>74</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>69</td>\n      <td>97</td>\n      <td>90</td>\n      <td>93</td>\n      <td>89</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 174,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用Pandas中的数据类型\n",
    "score_df = pd.DataFrame(score)\n",
    "score_df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a4c0020455e07c2b",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "给分数数据增加行列索引,显示效果更佳\n",
    "### 增加行、列索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 175,
   "id": "ba27ac6833a9c731",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:19.157416Z",
     "start_time": "2024-02-22T06:21:19.109122Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "     语文  数学  英语  政治  体育\n同学0  75  45  52  89  53\n同学1  86  64  76  47  95\n同学2  93  86  47  73  71\n同学3  60  70  68  40  70\n同学4  80  96  86  64  43\n同学5  60  53  77  57  76\n同学6  44  71  87  46  67\n同学7  74  49  80  99  71\n同学8  62  84  94  84  74\n同学9  69  97  90  93  89",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n      <th>政治</th>\n      <th>体育</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>同学0</th>\n      <td>75</td>\n      <td>45</td>\n      <td>52</td>\n      <td>89</td>\n      <td>53</td>\n    </tr>\n    <tr>\n      <th>同学1</th>\n      <td>86</td>\n      <td>64</td>\n      <td>76</td>\n      <td>47</td>\n      <td>95</td>\n    </tr>\n    <tr>\n      <th>同学2</th>\n      <td>93</td>\n      <td>86</td>\n      <td>47</td>\n      <td>73</td>\n      <td>71</td>\n    </tr>\n    <tr>\n      <th>同学3</th>\n      <td>60</td>\n      <td>70</td>\n      <td>68</td>\n      <td>40</td>\n      <td>70</td>\n    </tr>\n    <tr>\n      <th>同学4</th>\n      <td>80</td>\n      <td>96</td>\n      <td>86</td>\n      <td>64</td>\n      <td>43</td>\n    </tr>\n    <tr>\n      <th>同学5</th>\n      <td>60</td>\n      <td>53</td>\n      <td>77</td>\n      <td>57</td>\n      <td>76</td>\n    </tr>\n    <tr>\n      <th>同学6</th>\n      <td>44</td>\n      <td>71</td>\n      <td>87</td>\n      <td>46</td>\n      <td>67</td>\n    </tr>\n    <tr>\n      <th>同学7</th>\n      <td>74</td>\n      <td>49</td>\n      <td>80</td>\n      <td>99</td>\n      <td>71</td>\n    </tr>\n    <tr>\n      <th>同学8</th>\n      <td>62</td>\n      <td>84</td>\n      <td>94</td>\n      <td>84</td>\n      <td>74</td>\n    </tr>\n    <tr>\n      <th>同学9</th>\n      <td>69</td>\n      <td>97</td>\n      <td>90</td>\n      <td>93</td>\n      <td>89</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 175,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 构造列索引序列\n",
    "subjects = [\"语文\", \"数学\", \"英语\", \"政治\", \"体育\"]\n",
    "\n",
    "# 构造行索引序列\n",
    "stu = ['同学' + str(i) for i in range(score_df.shape[0])]\n",
    "\n",
    "# 添加行、列索引\n",
    "data = pd.DataFrame(score, columns=subjects, index=stu)\n",
    "data"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "623a0c3718850d71",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 2.2 DataFrame的属性\n",
    "### shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "id": "a7242194a9bb8ae1",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:19.257735800Z",
     "start_time": "2024-02-22T06:21:19.159415700Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "(10, 5)"
     },
     "execution_count": 176,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8a09eaca3d09d1df",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### index\n",
    "> DataFrame的行索引列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 177,
   "id": "bab99968e141dd71",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:19.323234100Z",
     "start_time": "2024-02-22T06:21:19.229237100Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['同学0', '同学1', '同学2', '同学3', '同学4', '同学5', '同学6', '同学7', '同学8', '同学9'], dtype='object')"
     },
     "execution_count": 177,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.index"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5c41d1e2ed195e4e",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### columns\n",
    "> DataFrame的行索引列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "id": "ab99cd1d1c7d1c91",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:19.351964Z",
     "start_time": "2024-02-22T06:21:19.310859400Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "Index(['语文', '数学', '英语', '政治', '体育'], dtype='object')"
     },
     "execution_count": 178,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1c9f9feafee0b928",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### values\n",
    "直接获取其中array的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 179,
   "id": "fa309bfbb535187b",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:19.402968400Z",
     "start_time": "2024-02-22T06:21:19.354140500Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "array([[75, 45, 52, 89, 53],\n       [86, 64, 76, 47, 95],\n       [93, 86, 47, 73, 71],\n       [60, 70, 68, 40, 70],\n       [80, 96, 86, 64, 43],\n       [60, 53, 77, 57, 76],\n       [44, 71, 87, 46, 67],\n       [74, 49, 80, 99, 71],\n       [62, 84, 94, 84, 74],\n       [69, 97, 90, 93, 89]])"
     },
     "execution_count": 179,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.values"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "671f702315145918",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### T\n",
    "> 转置"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 180,
   "id": "3abe7ee27f155014",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:19.425268700Z",
     "start_time": "2024-02-22T06:21:19.408184400Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "    同学0  同学1  同学2  同学3  同学4  同学5  同学6  同学7  同学8  同学9\n语文   75   86   93   60   80   60   44   74   62   69\n数学   45   64   86   70   96   53   71   49   84   97\n英语   52   76   47   68   86   77   87   80   94   90\n政治   89   47   73   40   64   57   46   99   84   93\n体育   53   95   71   70   43   76   67   71   74   89",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>同学0</th>\n      <th>同学1</th>\n      <th>同学2</th>\n      <th>同学3</th>\n      <th>同学4</th>\n      <th>同学5</th>\n      <th>同学6</th>\n      <th>同学7</th>\n      <th>同学8</th>\n      <th>同学9</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>语文</th>\n      <td>75</td>\n      <td>86</td>\n      <td>93</td>\n      <td>60</td>\n      <td>80</td>\n      <td>60</td>\n      <td>44</td>\n      <td>74</td>\n      <td>62</td>\n      <td>69</td>\n    </tr>\n    <tr>\n      <th>数学</th>\n      <td>45</td>\n      <td>64</td>\n      <td>86</td>\n      <td>70</td>\n      <td>96</td>\n      <td>53</td>\n      <td>71</td>\n      <td>49</td>\n      <td>84</td>\n      <td>97</td>\n    </tr>\n    <tr>\n      <th>英语</th>\n      <td>52</td>\n      <td>76</td>\n      <td>47</td>\n      <td>68</td>\n      <td>86</td>\n      <td>77</td>\n      <td>87</td>\n      <td>80</td>\n      <td>94</td>\n      <td>90</td>\n    </tr>\n    <tr>\n      <th>政治</th>\n      <td>89</td>\n      <td>47</td>\n      <td>73</td>\n      <td>40</td>\n      <td>64</td>\n      <td>57</td>\n      <td>46</td>\n      <td>99</td>\n      <td>84</td>\n      <td>93</td>\n    </tr>\n    <tr>\n      <th>体育</th>\n      <td>53</td>\n      <td>95</td>\n      <td>71</td>\n      <td>70</td>\n      <td>43</td>\n      <td>76</td>\n      <td>67</td>\n      <td>71</td>\n      <td>74</td>\n      <td>89</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 180,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "69ab56f163caa03b",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### head(5)：显示前5行内容\n",
    "> 如果不补充参数，默认5行。填入参数N则显示前N行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 181,
   "id": "6a306c0303f6a525",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:19.485915300Z",
     "start_time": "2024-02-22T06:21:19.426268700Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "     语文  数学  英语  政治  体育\n同学0  75  45  52  89  53\n同学1  86  64  76  47  95\n同学2  93  86  47  73  71\n同学3  60  70  68  40  70\n同学4  80  96  86  64  43",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n      <th>政治</th>\n      <th>体育</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>同学0</th>\n      <td>75</td>\n      <td>45</td>\n      <td>52</td>\n      <td>89</td>\n      <td>53</td>\n    </tr>\n    <tr>\n      <th>同学1</th>\n      <td>86</td>\n      <td>64</td>\n      <td>76</td>\n      <td>47</td>\n      <td>95</td>\n    </tr>\n    <tr>\n      <th>同学2</th>\n      <td>93</td>\n      <td>86</td>\n      <td>47</td>\n      <td>73</td>\n      <td>71</td>\n    </tr>\n    <tr>\n      <th>同学3</th>\n      <td>60</td>\n      <td>70</td>\n      <td>68</td>\n      <td>40</td>\n      <td>70</td>\n    </tr>\n    <tr>\n      <th>同学4</th>\n      <td>80</td>\n      <td>96</td>\n      <td>86</td>\n      <td>64</td>\n      <td>43</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 181,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "efbc9c8ff2dee540",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "### tail(5):显示后5行内容\n",
    "如果不补充参数，默认5行。填入参数N则显示后N行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 182,
   "id": "980d19e07921757b",
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:19.519267800Z",
     "start_time": "2024-02-22T06:21:19.477052600Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "     语文  数学  英语  政治  体育\n同学5  60  53  77  57  76\n同学6  44  71  87  46  67\n同学7  74  49  80  99  71\n同学8  62  84  94  84  74\n同学9  69  97  90  93  89",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n      <th>政治</th>\n      <th>体育</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>同学5</th>\n      <td>60</td>\n      <td>53</td>\n      <td>77</td>\n      <td>57</td>\n      <td>76</td>\n    </tr>\n    <tr>\n      <th>同学6</th>\n      <td>44</td>\n      <td>71</td>\n      <td>87</td>\n      <td>46</td>\n      <td>67</td>\n    </tr>\n    <tr>\n      <th>同学7</th>\n      <td>74</td>\n      <td>49</td>\n      <td>80</td>\n      <td>99</td>\n      <td>71</td>\n    </tr>\n    <tr>\n      <th>同学8</th>\n      <td>62</td>\n      <td>84</td>\n      <td>94</td>\n      <td>84</td>\n      <td>74</td>\n    </tr>\n    <tr>\n      <th>同学9</th>\n      <td>69</td>\n      <td>97</td>\n      <td>90</td>\n      <td>93</td>\n      <td>89</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 182,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data.tail(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "54658c33fe710368",
   "metadata": {
    "collapsed": false
   },
   "source": [
    "## 2.3 DatatFrame索引的设置\n",
    "### 2.3.1 修改行列索引值"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "      语文  数学  英语  政治  体育\n学生_0  75  45  52  89  53\n学生_1  86  64  76  47  95\n学生_2  93  86  47  73  71\n学生_3  60  70  68  40  70\n学生_4  80  96  86  64  43\n学生_5  60  53  77  57  76\n学生_6  44  71  87  46  67\n学生_7  74  49  80  99  71\n学生_8  62  84  94  84  74\n学生_9  69  97  90  93  89",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n      <th>政治</th>\n      <th>体育</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>学生_0</th>\n      <td>75</td>\n      <td>45</td>\n      <td>52</td>\n      <td>89</td>\n      <td>53</td>\n    </tr>\n    <tr>\n      <th>学生_1</th>\n      <td>86</td>\n      <td>64</td>\n      <td>76</td>\n      <td>47</td>\n      <td>95</td>\n    </tr>\n    <tr>\n      <th>学生_2</th>\n      <td>93</td>\n      <td>86</td>\n      <td>47</td>\n      <td>73</td>\n      <td>71</td>\n    </tr>\n    <tr>\n      <th>学生_3</th>\n      <td>60</td>\n      <td>70</td>\n      <td>68</td>\n      <td>40</td>\n      <td>70</td>\n    </tr>\n    <tr>\n      <th>学生_4</th>\n      <td>80</td>\n      <td>96</td>\n      <td>86</td>\n      <td>64</td>\n      <td>43</td>\n    </tr>\n    <tr>\n      <th>学生_5</th>\n      <td>60</td>\n      <td>53</td>\n      <td>77</td>\n      <td>57</td>\n      <td>76</td>\n    </tr>\n    <tr>\n      <th>学生_6</th>\n      <td>44</td>\n      <td>71</td>\n      <td>87</td>\n      <td>46</td>\n      <td>67</td>\n    </tr>\n    <tr>\n      <th>学生_7</th>\n      <td>74</td>\n      <td>49</td>\n      <td>80</td>\n      <td>99</td>\n      <td>71</td>\n    </tr>\n    <tr>\n      <th>学生_8</th>\n      <td>62</td>\n      <td>84</td>\n      <td>94</td>\n      <td>84</td>\n      <td>74</td>\n    </tr>\n    <tr>\n      <th>学生_9</th>\n      <td>69</td>\n      <td>97</td>\n      <td>90</td>\n      <td>93</td>\n      <td>89</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 183,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu = [\"学生_\" + str(i) for i in range(score_df.shape[0])]\n",
    "# 必须整体全部修改\n",
    "data.index = stu\n",
    "data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:19.576091300Z",
     "start_time": "2024-02-22T06:21:19.521778500Z"
    }
   },
   "id": "56ee0755ab2abb23",
   "execution_count": 183
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# 错误修改方式\n",
    "# data.index[3] = '学生_3'"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:19.667930400Z",
     "start_time": "2024-02-22T06:21:19.577090300Z"
    }
   },
   "id": "c0adc56c88fb887b",
   "execution_count": 184
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.3.2 重设索引\n",
    "reset_index(drop=False)\n",
    "- 设置新的下标索引\n",
    "- drop:默认为False，不删除原来索引，如果为True,删除原来的索引值"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "fc5d6d87cc20ad94"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "  index  语文  数学  英语  政治  体育\n0  学生_0  75  45  52  89  53\n1  学生_1  86  64  76  47  95\n2  学生_2  93  86  47  73  71\n3  学生_3  60  70  68  40  70\n4  学生_4  80  96  86  64  43\n5  学生_5  60  53  77  57  76\n6  学生_6  44  71  87  46  67\n7  学生_7  74  49  80  99  71\n8  学生_8  62  84  94  84  74\n9  学生_9  69  97  90  93  89",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>index</th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n      <th>政治</th>\n      <th>体育</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>学生_0</td>\n      <td>75</td>\n      <td>45</td>\n      <td>52</td>\n      <td>89</td>\n      <td>53</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>学生_1</td>\n      <td>86</td>\n      <td>64</td>\n      <td>76</td>\n      <td>47</td>\n      <td>95</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>学生_2</td>\n      <td>93</td>\n      <td>86</td>\n      <td>47</td>\n      <td>73</td>\n      <td>71</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>学生_3</td>\n      <td>60</td>\n      <td>70</td>\n      <td>68</td>\n      <td>40</td>\n      <td>70</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>学生_4</td>\n      <td>80</td>\n      <td>96</td>\n      <td>86</td>\n      <td>64</td>\n      <td>43</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>学生_5</td>\n      <td>60</td>\n      <td>53</td>\n      <td>77</td>\n      <td>57</td>\n      <td>76</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>学生_6</td>\n      <td>44</td>\n      <td>71</td>\n      <td>87</td>\n      <td>46</td>\n      <td>67</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>学生_7</td>\n      <td>74</td>\n      <td>49</td>\n      <td>80</td>\n      <td>99</td>\n      <td>71</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>学生_8</td>\n      <td>62</td>\n      <td>84</td>\n      <td>94</td>\n      <td>84</td>\n      <td>74</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>学生_9</td>\n      <td>69</td>\n      <td>97</td>\n      <td>90</td>\n      <td>93</td>\n      <td>89</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 185,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 重置索引,drop=False\n",
    "data.reset_index()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:19.712087Z",
     "start_time": "2024-02-22T06:21:19.666419900Z"
    }
   },
   "id": "5f32a1836a44f89c",
   "execution_count": 185
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   语文  数学  英语  政治  体育\n0  75  45  52  89  53\n1  86  64  76  47  95\n2  93  86  47  73  71\n3  60  70  68  40  70\n4  80  96  86  64  43\n5  60  53  77  57  76\n6  44  71  87  46  67\n7  74  49  80  99  71\n8  62  84  94  84  74\n9  69  97  90  93  89",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>语文</th>\n      <th>数学</th>\n      <th>英语</th>\n      <th>政治</th>\n      <th>体育</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>75</td>\n      <td>45</td>\n      <td>52</td>\n      <td>89</td>\n      <td>53</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>86</td>\n      <td>64</td>\n      <td>76</td>\n      <td>47</td>\n      <td>95</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>93</td>\n      <td>86</td>\n      <td>47</td>\n      <td>73</td>\n      <td>71</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>60</td>\n      <td>70</td>\n      <td>68</td>\n      <td>40</td>\n      <td>70</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>80</td>\n      <td>96</td>\n      <td>86</td>\n      <td>64</td>\n      <td>43</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>60</td>\n      <td>53</td>\n      <td>77</td>\n      <td>57</td>\n      <td>76</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>44</td>\n      <td>71</td>\n      <td>87</td>\n      <td>46</td>\n      <td>67</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>74</td>\n      <td>49</td>\n      <td>80</td>\n      <td>99</td>\n      <td>71</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>62</td>\n      <td>84</td>\n      <td>94</td>\n      <td>84</td>\n      <td>74</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>69</td>\n      <td>97</td>\n      <td>90</td>\n      <td>93</td>\n      <td>89</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 186,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 重置索引,drop=True\n",
    "data.reset_index(drop=True)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:19.759183900Z",
     "start_time": "2024-02-22T06:21:19.714089200Z"
    }
   },
   "id": "98cdb5ea6a9ad938",
   "execution_count": 186
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 2.3.3 某列值设置为新的索引\n",
    "set_index(keys, drop=True)\n",
    "- keys : 列索引名成或者列索引名称的列表\n",
    "- drop : boolean, default True.当做新的索引，删除原来的列\n",
    "设置新索引案例\n",
    "1、创建"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "7a49a8e6d729fa2b"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "   month  year  sale\n0      1  2012    55\n1      4  2014    40\n2      7  2013    84\n3     10  2014    31",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>month</th>\n      <th>year</th>\n      <th>sale</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>2012</td>\n      <td>55</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4</td>\n      <td>2014</td>\n      <td>40</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>7</td>\n      <td>2013</td>\n      <td>84</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>10</td>\n      <td>2014</td>\n      <td>31</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 187,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.DataFrame({'month': [1, 4, 7, 10],\n",
    "                    'year': [2012, 2014, 2013, 2014],\n",
    "                    'sale':[55, 40, 84, 31]})\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:19.799115700Z",
     "start_time": "2024-02-22T06:21:19.751647900Z"
    }
   },
   "id": "f48a1d5dc0a63ca1",
   "execution_count": 187
  },
  {
   "cell_type": "markdown",
   "source": [
    "2、以月份设置新的索引"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "53face5a7ff91b31"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "       year  sale\nmonth            \n1      2012    55\n4      2014    40\n7      2013    84\n10     2014    31",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>year</th>\n      <th>sale</th>\n    </tr>\n    <tr>\n      <th>month</th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1</th>\n      <td>2012</td>\n      <td>55</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>2014</td>\n      <td>40</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>2013</td>\n      <td>84</td>\n    </tr>\n    <tr>\n      <th>10</th>\n      <td>2014</td>\n      <td>31</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 188,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.set_index('month')"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:19.853849200Z",
     "start_time": "2024-02-22T06:21:19.794310400Z"
    }
   },
   "id": "3941751c3145d72d",
   "execution_count": 188
  },
  {
   "cell_type": "markdown",
   "source": [
    "3、设置多个索引，以年和月份"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "24222561a6ba6b18"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "df = df.set_index(['year', 'month'])"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:19.920949800Z",
     "start_time": "2024-02-22T06:21:19.855850Z"
    }
   },
   "id": "ff5d2c1efffa0ad1",
   "execution_count": 189
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "            sale\nyear month      \n2012 1        55\n2014 4        40\n2013 7        84\n2014 10       31",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>sale</th>\n    </tr>\n    <tr>\n      <th>year</th>\n      <th>month</th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>2012</th>\n      <th>1</th>\n      <td>55</td>\n    </tr>\n    <tr>\n      <th>2014</th>\n      <th>4</th>\n      <td>40</td>\n    </tr>\n    <tr>\n      <th>2013</th>\n      <th>7</th>\n      <td>84</td>\n    </tr>\n    <tr>\n      <th>2014</th>\n      <th>10</th>\n      <td>31</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 190,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:20.005965800Z",
     "start_time": "2024-02-22T06:21:19.922955200Z"
    }
   },
   "id": "a7fae25493b32d0e",
   "execution_count": 190
  },
  {
   "cell_type": "markdown",
   "source": [
    "> 注：通过刚才的设置，这样DataFrame就变成了一个具有MultiIndex的DataFrame。"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "11bbe3f81f14ae0f"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 3.MultiIndex与Panel\n",
    "## 3.1 MultiIndex\n",
    "MultiIndex是三维的数据类型;\n",
    "\n",
    "多级索引（也称层次化索引）是pandas的重要功能，可以在Series、DataFrame对象上拥有2个以及2个以上的索引。\n",
    "### 3.1.1 multiIndex的特性\n",
    "打印刚才的df的行索引结果"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "fba69f1aee9e130a"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "MultiIndex([(2012,  1),\n            (2014,  4),\n            (2013,  7),\n            (2014, 10)],\n           names=['year', 'month'])"
     },
     "execution_count": 191,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:20.084420200Z",
     "start_time": "2024-02-22T06:21:19.985226900Z"
    }
   },
   "id": "8ef83ec89f95497a",
   "execution_count": 191
  },
  {
   "cell_type": "markdown",
   "source": [
    "多级或分层索引对象。\n",
    "- index属性\n",
    "- names:levels的名称\n",
    "- levels：每个level的元组值"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "988c0d736709b736"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "FrozenList(['year', 'month'])"
     },
     "execution_count": 192,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index.names"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:20.112320700Z",
     "start_time": "2024-02-22T06:21:20.067036700Z"
    }
   },
   "id": "69c48b684fa6315b",
   "execution_count": 192
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "FrozenList([[2012, 2013, 2014], [1, 4, 7, 10]])"
     },
     "execution_count": 193,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.index.levels"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:20.180962300Z",
     "start_time": "2024-02-22T06:21:20.114325600Z"
    }
   },
   "id": "b0eae981610c7f0f",
   "execution_count": 193
  },
  {
   "cell_type": "markdown",
   "source": [
    "### 3.1.2 multiIndex的创建"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "5d1019e06e500ca4"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "arrays = [[1, 1, 2, 2], ['red', 'blue', 'red', 'blue']]\n",
    "mi_index = pd.MultiIndex.from_arrays(arrays, names=('number', 'color'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:20.247821800Z",
     "start_time": "2024-02-22T06:21:20.183397Z"
    }
   },
   "id": "9854758a8b3f94e7",
   "execution_count": 194
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": "                     0         1         2         3\nnumber color                                        \n1      red    0.385015 -0.048674 -0.168452  0.725372\n       blue  -1.317499 -1.532280 -0.404872  0.733324\n2      red    0.011122  0.502736  0.951611  1.019517\n       blue   0.931186  0.648056 -3.066351 -1.053383",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>2</th>\n      <th>3</th>\n    </tr>\n    <tr>\n      <th>number</th>\n      <th>color</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">1</th>\n      <th>red</th>\n      <td>0.385015</td>\n      <td>-0.048674</td>\n      <td>-0.168452</td>\n      <td>0.725372</td>\n    </tr>\n    <tr>\n      <th>blue</th>\n      <td>-1.317499</td>\n      <td>-1.532280</td>\n      <td>-0.404872</td>\n      <td>0.733324</td>\n    </tr>\n    <tr>\n      <th rowspan=\"2\" valign=\"top\">2</th>\n      <th>red</th>\n      <td>0.011122</td>\n      <td>0.502736</td>\n      <td>0.951611</td>\n      <td>1.019517</td>\n    </tr>\n    <tr>\n      <th>blue</th>\n      <td>0.931186</td>\n      <td>0.648056</td>\n      <td>-3.066351</td>\n      <td>-1.053383</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 195,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mi_df = pd.DataFrame(np.random.normal(size=(4,4)),index=mi_index)\n",
    "mi_df"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:20.265901400Z",
     "start_time": "2024-02-22T06:21:20.245998100Z"
    }
   },
   "id": "c18f443ec71ec960",
   "execution_count": 195
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.2 Panel\n",
    "### 3.2.1 panel的创建\n",
    "class pandas.Panel(data=None, items=None, major_axis=None, minor_axis=None)\n",
    "作用：存储3维数组的Panel类型\n",
    "\n",
    "- 参数：\n",
    "- data : ndarray或者dataframe\n",
    "- items : 索引或类似数组的对象，axis=0\n",
    "- major_axis : 索引或类似数组的对象，axis=1\n",
    "- minor_axis : 索引或类似数组的对象，axis=2\n",
    "\n",
    "> 注：==Panel从版本0.20.0开始弃用==：推荐的用于表示3D数据的方法是通过DataFrame上的MultiIndex方法"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "d5ae7537603a89ea"
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "# p = pd.Panel(data=np.arange(24).reshape(4,3,2),\n",
    "#                  items=list('ABCD'),\n",
    "#                  major_axis=pd.date_range('20130101', periods=3),\n",
    "#                  minor_axis=['first', 'second'])\n",
    "# AttributeError: module 'pandas' has no attribute 'Panel'"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T06:21:20.274627500Z",
     "start_time": "2024-02-22T06:21:20.267868800Z"
    }
   },
   "id": "b9e597968b9b0a72",
   "execution_count": 196
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 4 小结\n",
    "- pandas的优势【了解】\n",
    "    - 增强图表可读性\n",
    "    - 便捷的数据处理能力\n",
    "    - 读取文件方便\n",
    "    - 封装了Matplotlib、Numpy的画图和计算\n",
    "- series【知道】\n",
    "    - 创建\n",
    "        - pd.Series([], index=[])\n",
    "        - pd.Series({})\n",
    "    - 属性\n",
    "        - 对象.index\n",
    "        - 对象.values\n",
    "- DataFrame【掌握】\n",
    "    - 创建\n",
    "        - pd.DataFrame(data=None, index=None, columns=None)\n",
    "    - 属性\n",
    "        - shape -- 形状\n",
    "        - index -- 行索引\n",
    "        - columns -- 列索引\n",
    "        - values -- 查看值\n",
    "        - T -- 转置\n",
    "        - head() -- 查看头部内容\n",
    "        - tail() -- 查看尾部内容\n",
    "    - DataFrame索引\n",
    "        - 修改的时候,需要进行全局修改\n",
    "        - 对象.reset_index()\n",
    "        - 对象.set_index(keys)\n",
    "- MultiIndex与Panel【了解】\n",
    "    - multiIndex:\n",
    "        - 类似ndarray中的三维数组\n",
    "        - 创建：\n",
    "            - pd.MultiIndex.from_arrays()\n",
    "    - 属性：\n",
    "        - 对象.index\n",
    "    - panel：\n",
    "        - pd.Panel(data, items, major_axis, minor_axis)\n",
    "        - panel数据要是想看到,则需要进行索引到dataframe或者series才可以\n",
    "        > 注：==Panel从版本0.20.0开始弃用==：推荐的用于表示3D数据的方法是通过DataFrame上的MultiIndex方法"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "7b84077c3cab865c"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
